package com.baidu.thirdlib.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.baidu.thirdlib.BaseActivity;
import com.baidu.thirdlib.R;
import com.baidu.thirdlib.receiver.NotifyPushReceiver;
import com.kvin.toolkit.utils.receiver.OnReceiveListener;
import com.sqm.jpushlib.JPushUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

public class JPushActivity extends BaseActivity {

    @BindView(R.id.launch_btn)
    Button launchBtn;

    private NotifyPushReceiver mPushReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jpush);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        JPushInterface.resumePush(this);
        mPushReceiver = new NotifyPushReceiver();
        mPushReceiver.setOnReceiveListener(mOnReceiveListener);

        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(JPushUtils.PUSH_REGISTER);
        mFilter.addAction(JPushUtils.PUSH_MSG_RECEIVER);
        mFilter.addAction(JPushUtils.PUSH_NOTIFY_OPEN);
        mFilter.addAction(JPushUtils.PUSH_NOTIFY_RECEIVER);
        mFilter.addAction(JPushUtils.PUSH_NET_CHECK);
        mFilter.addCategory("com.baidu.thirdlib");
        registerReceiver(mPushReceiver, mFilter);


    }


    /**
     * handle clicking events  here
     */
    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.launch_btn:
                    break;
            }
        }
    };

    private OnReceiveListener mOnReceiveListener = new OnReceiveListener() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //handle push msg here
            String ss = intent.getDataString();
            String title = intent.getStringExtra(JPushInterface.EXTRA_NOTIFICATION_TITLE);//common 消息名
            String msg = intent.getStringExtra(JPushInterface.EXTRA_TITLE);
            String extra = intent.getStringExtra(JPushInterface.EXTRA_MESSAGE);//custom 消息主体
            String msg1 = intent.getStringExtra(JPushInterface.EXTRA_EXTRA);//custom 附加消息

        }
    };


    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mPushReceiver);
    }
}
